package com.zhanghu.backendZray.controller.dailyPlan;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhanghu.backendZray.annotation.MenuNeed;
import com.zhanghu.backendZray.pojo.dto.ApiResponse;
import com.zhanghu.backendZray.pojo.entity.DailyPlan;
import com.zhanghu.backendZray.pojo.request.dailyPlanTask.DailyPlanPageRequest;
import com.zhanghu.backendZray.pojo.request.dailyPlanTask.DailyPlanRequest;
import com.zhanghu.backendZray.pojo.request.OnlyIdRequest;
import com.zhanghu.backendZray.pojo.vo.DailyPlanVo;
import com.zhanghu.backendZray.service.DailyPlanService;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 日常计划
 *
 * @author ZhangDong
 * @date 2024/7/7 18:14
 */
@RestController
@RequestMapping("dailyPlan")
@AllArgsConstructor
public class DailyPlanController {

    private final DailyPlanService dailyPlanService;

    @MenuNeed(menuName = "plan_list")
    @PostMapping("add")
    public ApiResponse add(@Validated(DailyPlanRequest.Create.class) @RequestBody DailyPlanRequest dailyPlanRequest) {
        dailyPlanService.add(dailyPlanRequest);
        return ApiResponse.ok();
    }

    @MenuNeed(menuName = "plan_list")
    @PostMapping("createTask")
    public ApiResponse createTask(@RequestBody OnlyIdRequest request) {
        dailyPlanService.createTask(request.getId());
        return ApiResponse.ok();
    }

    @MenuNeed(menuName = "plan_list")
    @PutMapping("update")
    public ApiResponse update(@Validated(DailyPlanRequest.Update.class) @RequestBody DailyPlanRequest dailyPlanRequest) {
        dailyPlanService.update(dailyPlanRequest);
        return ApiResponse.ok();
    }

    @MenuNeed(menuName = "plan_list")
    @PostMapping("delete")
    public ApiResponse delete(@RequestBody @NotNull OnlyIdRequest request) {
        Assert.notNull(request, "id不能为空");
        dailyPlanService.del(request.getId());
        return ApiResponse.ok();
    }

    /**
     * 分页查询
     *
     * @param dailyPlanRequest
     * @return
     */
    @MenuNeed(menuName = "plan_list")
    @PostMapping("page")
    public ApiResponse<Page<DailyPlanVo>> page(@RequestBody DailyPlanPageRequest dailyPlanRequest) {
        Page<DailyPlanVo> pageInfo = dailyPlanService.page(dailyPlanRequest);
        return ApiResponse.ok(pageInfo);
    }

    @MenuNeed(menuName = "plan_list")
    @PostMapping("getById")
    public ApiResponse<DailyPlan> page(@RequestBody OnlyIdRequest request) {
        DailyPlan dailyPlan = dailyPlanService.getById(request.getId());
        return ApiResponse.ok(dailyPlan);
    }
}
